package com.tsing.service.impl;

import com.tsing.common.result.PageResult;
import com.tsing.mapper.ArticleMapper;
import com.tsing.mapper.LetterMapper;
import com.tsing.mapper.impl.ArticleMapperImpl;
import com.tsing.mapper.impl.LetterMapperImpl;
import com.tsing.pojo.entity.TlLetter;
import com.tsing.pojo.entity.TlUser;
import com.tsing.pojo.vo.LetterVO;
import com.tsing.service.LetterService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/15 20:51
 */
public class LetterServiceImpl implements LetterService {
    private final LetterMapper letterMapper = new LetterMapperImpl();
    private final ArticleMapper articleMapper = new ArticleMapperImpl();

    @Override
    public PageResult<LetterVO> list(Integer page, Integer size, Integer userId, Integer senderId) {
//        TlUser sender = articleMapper.getUserById(senderId);
        PageResult<TlLetter> list = letterMapper.list(page, size, userId, senderId);

        List<LetterVO> letterVOList = new ArrayList<>();
        for (TlLetter letter : list.getData()) {
            LetterVO letterVO = new LetterVO();
            TlUser user = articleMapper.getUserById(Integer.parseInt(letter.getUserId().toString()));

            letterVO.setUserId(Long.parseLong(letter.getUserId().toString()));

            letterVO.setSenderId(Long.parseLong(letter.getSenderId().toString()));

//            letterVO.setSenderName(sender.getUsername());
            letterVO.setUserName(user.getUsername());
            letterVO.setUserAvatar(user.getUserImage());
//            letterVO.setSenderAvatar(sender.getUserImage());
            letterVO.setCreatedTime(letter.getCreatedTime());
            letterVO.setContent(letter.getContent());
            letterVOList.add(letterVO);
        }

        PageResult<LetterVO> result = new PageResult<>();
        result.setCode(200);
        result.setData(letterVOList);

        return result;
    }


    @Override
    public Integer send(Integer userId, Integer senderId, String content) {
        TlLetter letter = new TlLetter();

        letter.setContent(content);
        letter.setCreatedTime(LocalDateTime.now());
        letter.setSenderId(Long.parseLong(senderId.toString()));
        letter.setUserId(Long.parseLong(userId.toString()));

        return letterMapper.send(letter);
    }

    @Override
    public PageResult<LetterVO> latestMessage(Integer page, Integer size, Integer userId) {
        PageResult<TlLetter> list = letterMapper.latestMessage(page, size, userId);

        List<LetterVO> letterVOList = new ArrayList<>();
        for (TlLetter letter : list.getData()) {
            LetterVO letterVO = new LetterVO();
            TlUser user = articleMapper.getUserById(Integer.parseInt(letter.getUserId().toString()));

            letterVO.setUserId(Long.parseLong(letter.getUserId().toString()));

//            letterVO.setSenderId(Long.parseLong(letter.getSenderId().toString()));

//            letterVO.setSenderName(sender.getUsername());
            letterVO.setUserName(user.getUsername());
            letterVO.setUserAvatar(user.getUserImage());
//            letterVO.setSenderAvatar(sender.getUserImage());
            String createdTime = letter.getCreatedTime().toString();
            letterVO.setCreatedTime(LocalDateTime.parse(createdTime));
            letterVO.setContent(letter.getContent());
            letterVOList.add(letterVO);
        }

        PageResult<LetterVO> result = new PageResult<>();
        result.setCode(200);
        result.setData(letterVOList);

        return result;
    }
}
